Imports Vintasoft.Imaging
Imports Vintasoft.Twain
Imports System.Windows.Forms

Namespace Twain
	''' <summary>
	''' Represents a simple TWAIN manager.
	''' </summary>
	Public Class SimpleTwainManager

		#Region "Fields"

		''' <summary>
		''' Images.
		''' </summary>
		Private _images As ImageCollection

		''' <summary>
		''' TWAIN device manager.
		''' </summary>
		Private _deviceManager As DeviceManager

		''' <summary>
		''' Current TWAIN device.
		''' </summary>
		Private _device As Device

		''' <summary>
		''' Main form.
		''' </summary>
		Private _mainForm As Form

		#End Region



		#Region "Constructors"

		Public Sub New(mainForm As Form, images As ImageCollection)
			_images = images
			_mainForm = mainForm
		End Sub

		#End Region



		#Region "Methods"

		Public Sub SelectDeviceAndAcquireImage()
            Try
                If _deviceManager Is Nothing Then
                    _deviceManager = New DeviceManager(_mainForm)
                End If

                _deviceManager.Open()

                If Not _deviceManager.Devices.[Select]() Then
                    _deviceManager.Close()
                    Return
                End If

                _device = _deviceManager.Devices.Current

                AddHandler _device.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf _device_ImageAcquired)
                AddHandler _device.ScanCompleted, New EventHandler(AddressOf _device_ScanCompleted)
                AddHandler _device.ScanCanceled, New EventHandler(AddressOf _device_ScanCanceled)
                AddHandler _device.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf _device_ScanFailed)
                AddHandler _device.UserInterfaceClosed, New EventHandler(AddressOf _device_UserInterfaceClosed)

                _device.Acquire()
            Catch ex As TwainInvalidStateException
                DemosTools.ShowErrorMessage(ex)
            Catch ex As TwainDeviceManagerException
                DemosTools.ShowErrorMessage(ex)
            Catch ex As TwainDeviceException
                DemosTools.ShowErrorMessage(ex)
            End Try
        End Sub

		Private Sub _device_ImageAcquired(sender As Object, e As ImageAcquiredEventArgs)
			SyncLock Me
				#If STORE_ACQUIRE_IMAGE_ON_DISK Then
				' Store acquired image on the disk

				Dim imagesDir As String = Path.Combine(Directory.GetCurrentDirectory(), "Images")
				If Not Directory.Exists(imagesDir) Then
					Directory.CreateDirectory(imagesDir)
				End If

				Dim imageFilename As String = Path.Combine(imagesDir, String.Format("{0}.png", DateTime.Now.Ticks))

				Try
					e.Image.Save(imageFilename)
				Catch ex As Exception
					DemosTools.ShowErrorMessage(ex)
					Return
				End Try

				_images.Add(imageFilename)

				#Else
				' Store acquired image in the memory

				Try
					_images.Add(New VintasoftImage(e.Image.GetAsBitmap(), True))
				Catch ex As Exception
					DemosTools.ShowErrorMessage(ex)
					Return
					#End If
				End Try
			End SyncLock
		End Sub

		Private Sub _device_ScanCompleted(sender As Object, e As EventArgs)
			CloseScanner()
		End Sub

		Private Sub _device_ScanCanceled(sender As Object, e As EventArgs)
			CloseScanner()

			DemosTools.ShowWarningMessage("Scan canceled.")
		End Sub

		Private Sub _device_ScanFailed(sender As Object, e As ScanFailedEventArgs)
			CloseScanner()

			DemosTools.ShowWarningMessage("Scan failed.")
		End Sub

		Private Sub _device_UserInterfaceClosed(sender As Object, e As EventArgs)
			CloseScanner()
		End Sub

		Private Sub CloseScanner()
			If _device.State = DeviceState.Opened Then
				_device.Close()
			End If

			If _deviceManager.State = DeviceManagerState.Opened Then
				_deviceManager.Close()
			End If
		End Sub

		#End Region

	End Class
End Namespace
